package com.chj.gaoji.class14;

// https://blog.csdn.net/weixin_42977872/article/details/84592396
public class Code05_Discretization {
	public static class Node implements Comparable<Node> {
		int rt;
		int idx;

		public Node(int rt, int idx) {
			this.rt = rt;
			this.idx = idx;
		}

		@Override
		public int compareTo(Node node) {
			return rt - node.rt;
		}
	}

	public static void work(int[] array) {
		int[] rank = new int[array.length];
		Node[] nodes = new Node[array.length];
		for (int i = 0; i < array.length; ++i) {
			nodes[i] = new Node(array[i], i); // 传入数值和坐标
		}
		java.util.Arrays.sort(nodes); // 排序 记得实现Comparable接口 以rt大小排序
		for (int i = 0; i < array.length; ++i) {
			rank[nodes[i].idx] = i;
		}
		for (int i = 0; i < array.length; ++i) {
			array[i] = rank[i] + 1;
		}
		// 10000000,2,2,123123213 离散化成 3 1 2 4
	}

	public static void main(String[] args) {
		int[] a = { 10000000, 2, 2, 123123213 };
		work(a);

//		[3, 1, 2, 4]
		System.out.println(a);
	}
}
